﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices;
using Microsoft.SqlServer.Dts.Runtime;

namespace TVProgramAnalyst.ReportForm
{
    public partial class frmProcessSSASResult : Form
    {
        public frmProcessSSASResult()
        {
            InitializeComponent();
        }

        class MyEventListener : DefaultEvents
        {
            public override bool OnError(DtsObject source, int errorCode, string subComponent,
              string description, string helpFile, int helpContext, string idofInterfaceWithError)
            {
                // Add application-specific diagnostics here.
                MessageBox.Show(string.Format("Error in {0}/{1} : {2}", source, subComponent, description));
                return false;
            }
        }

        private void frmProcessSSASResult_Load(object sender, EventArgs e)
        {
            textBox1.Text = "Executing DTS Package ......";
            timer1.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            //Execute DTS Package
            string pkgLocation;
            Package pkg;
            Microsoft.SqlServer.Dts.Runtime.Application app;
            DTSExecResult pkgResults;
            MyEventListener eventListener = new MyEventListener();
            
            pkgLocation = System.Windows.Forms.Application.StartupPath+@"\TPA_ETL.dtsx";
            app = new Microsoft.SqlServer.Dts.Runtime.Application();

            pkg = app.LoadPackage(pkgLocation, eventListener);
            pkg.PackagePassword = "123456";

            pkgResults = pkg.Execute(null, null, eventListener, null, null);
            textBox1.Text += "DTS Package "+pkg.ExecutionResult.ToString();

            //Process Analysis Database
            Server server = new Server();
            server.Connect("Provider=MSOLAP.4;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=TPA_SSASP;Data Source=.");
            //server.Databases["TPA_SSASP"].Process(ProcessType.ProcessFull);
            foreach (Dimension d in server.Databases["TPA_SSASP"].Dimensions)
            {
                d.Process(ProcessType.ProcessFull);
                textBox1.Text += d.Name + " processed successfully.\r\n";
            }
            foreach (Cube c in server.Databases["TPA_SSASP"].Cubes)
            {
                c.Process(ProcessType.ProcessFull);
                textBox1.Text += "Cube " + c.Name + " processed successfully. \r\n";
            }
            server.Disconnect();
            Cursor = Cursors.Arrow;
            timer1.Enabled = false;
        }
    }
}
